package com.service.finanial.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.client.model.pojo.financial.AiruiFinancialUser;
import com.client.model.pojo.financial.FinancialItem;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import com.service.finanial.service.AiruiFinancialUserService;
import com.service.finanial.service.FinancialItemService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author：刘文举
 * @DateTime： 2024/4/10 20:02
 * @PackageName: com.service.finanial.controller
 * @ClassName: FinanController
 */
@RestController
@RequestMapping("/airui/api/financial")
public class FinanController {


    @Autowired
    private FinancialItemService financialItemService;

    @Autowired
    private AiruiFinancialUserService airuiFinancialUserService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;





    @GetMapping("/getFinancia")
    public BigDecimal getFinancia(@RequestParam(name = "token") String token) {
        ArUser arUser = JSON.parseObject(redisTemplate.opsForValue().get("user:login" + token), ArUser.class);
        if (arUser != null) {
            List<AiruiFinancialUser> list = airuiFinancialUserService.list(new QueryWrapper<AiruiFinancialUser>()
                    .eq("uid", arUser.getUid()));
            BigDecimal financiaPrice = new BigDecimal(0);
            for (AiruiFinancialUser m : list){
                FinancialItem id = financialItemService.getOne(new QueryWrapper<FinancialItem>()
                        .eq("id", m.getItemId()));
              financiaPrice = financiaPrice.add(id.getStartMoney().multiply(new BigDecimal(m.getRemark())));
            }
            return financiaPrice;
        }
        return new BigDecimal(0);
    }
    /**
     * 添加购买记录
     */
    @PostMapping("/save")
    public Result save( @RequestBody AiruiFinancialUser airuiFinancialUser){
        return airuiFinancialUserService.save(airuiFinancialUser)?Result.build(null,200,"添加成功"):Result.build(null,400,"添加失败");
    }
}
